﻿#include "checkthread.h"
#include "tools.h"
#include <QDebug>
#include <QFileInfo>
#include <QTextCodec>
#include <QDateTime>

void CheckThread::cbParsingProgress(char *filepath, uint8_t dwProgress, void *pContent)
{
    if (pContent)
    {
       ((CheckThread*)pContent)->onProgress(filepath, dwProgress);
    }
}

CheckThread::CheckThread(QObject *parent) : QThread(parent)
{
    m_bStart = false;
}

void CheckThread::SetFilePath(QString strFilePath)
{
    m_strFilePath = strFilePath;
    m_bStart = true;
}

void CheckThread::stop()
{
    requestInterruption();
}

void CheckThread::onProgress(char *filepath, uint8_t dwProgress)
{
    emit SignalProgress(m_eStep, dwProgress);
}

static QByteArray Utf8ToGb2312(QString strUtf8)
{
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QTextCodec::setCodecForLocale(utf8);

    QTextCodec* gbk = QTextCodec::codecForName("gbk");

    //1. utf8 -> unicode

    QString strUnicode= utf8->toUnicode(strUtf8.toLocal8Bit().data());

    //2. unicode -> gbk, 得到QByteArray
    return gbk->fromUnicode(strUnicode);
}

static QString GetSizeStr(qint64 dwSize)
{
    QString strSize = "";
    if (dwSize < 1024)
    {
        strSize = QString("%1 Bytes").arg(dwSize);
    }
    else if (dwSize < 1024 * 1024)
    {
        strSize = strSize.sprintf("%.2fKB", (double)dwSize / 1024);
    }
    else if (dwSize < 1024 * 1024 * 1024)
    {
        strSize = strSize.sprintf("%.2fMB", (double)dwSize / 1024 / 1024);
    }
    else if (dwSize < 1024 * 1024 * 1024 * 1024)
    {
        strSize = strSize.sprintf("%.2fGB", (double)dwSize / 1024 / 1024 / 1024);
    }
    else
    {
        strSize = strSize.sprintf("%.2fTB", (double)dwSize / 1024 / 1024 / 1024 / 1024);
    }

    return strSize;
}

void CheckThread::run()
{
    while (!isInterruptionRequested())
    {
        if (m_bStart)
        {
            m_eStep = CHECK_START;
            emit SignalProgress(m_eStep, 0);

            QFileInfo info(m_strFilePath);
            if (info.exists())
            {
                qDebug() << info.fileName() << info.size() << info.created().toString("yyyy-MM-dd hh:mm:ss")
                         << info.lastModified().toString("yyyy-MM-dd hh:mm:ss")
                         << info.lastRead().toString("yyyy-MM-dd hh:mm:ss");

                m_tFileInfo.strFilePath = info.filePath();
                m_tFileInfo.strFileName = info.fileName();
                m_tFileInfo.strFormat = info.suffix();
                m_tFileInfo.strModifyTime = info.lastModified().toString("yyyy-MM-dd hh:mm:ss");
                m_tFileInfo.strCreateTime = info.created().toString("yyyy-MM-dd hh:mm:ss");
                m_tFileInfo.strLastReadTime = info.lastRead().toString("yyyy-MM-dd hh:mm:ss");
                m_tFileInfo.dwSize = info.size();
                m_tFileInfo.strSize = GetSizeStr(info.size());
            }

            char file[1024] = {0};
            strncpy_s(file, Utf8ToGb2312(m_strFilePath).data(), sizeof(file) - 1);

            TFileParam tItem = {0};
            tItem.filepath = file;
            tItem.pFun = cbParsingProgress;
            tItem.pContent = this;

            m_eStep = CHECK_MD5;
            Md5FileComputer(tItem);
            qDebug("Md5FileComputer:%s\r\n\n", tItem.output);
            m_tFileInfo.strMd5 = tItem.output;

            m_eStep = CHECK_SHA1;
            Sha1FileComputer(tItem);
            qDebug("Sha1FileComputer:%s\r\n\n", tItem.output);
            m_tFileInfo.strSha1 = tItem.output;

            m_eStep = CHECK_OVER;
            emit SignalProgress(m_eStep, 0);
            m_bStart = false;
        }
        else
        {
            msleep(100);
        }
    }
}
